//=============================================================================
// Protector.
//=============================================================================

class TFGuardian extends TFStation
	placeable;

var float LastShot;

replication
{
	// Things the server should send to the client.
	reliable if ( Role == ROLE_Authority )
		LastShot;
}

state Ready
{
    function BeginState()
    {
        super.BeginState();

        if(myteam==0) setstaticmesh(StaticMesh'TFMedia.Buildings.tfGuardianRed');
        else setstaticmesh(StaticMesh'TFMedia.Buildings.tfGuardianBlu');

        settimer(0.5,true);
    }

    function Timer()
    {
        if(level.timeseconds-lastshot>(2.5+frand())/grade)
	    {
	        lastshot=level.timeseconds;
            findtarget();
	    }
    }

    simulated function Findtarget()
    {
		local controller p;
		local int t;
		local float range;
		local actor HitActor, targ;
        local vector HitNormal, HitLocation;

        targ=none;

        if(grade<3) range=float(tfgame(level.Game).outpostdist)*48;
        else range=float(tfgame(level.Game).outpostdist)*128;

		for ( P = Level.ControllerList; P!=None; P=P.nextController )
        {
            if(p.pawn==none) continue;
		    if(p.playerreplicationinfo!=none) t=p.pawn.playerreplicationinfo.team.teamindex;
            foreach TraceActors(class'Actor', HitActor, HitLocation, HitNormal, p.pawn.location+p.Pawn.EyePosition(), location)
        	{
        		if(HitActor == None || Volume(HitActor)!=none)
        			continue;

        		//Don't hit shield
        		if(BigShieldSphere(HitActor)!=none)
                    continue;

                break;
        	}
            //if(pawn(HitActor)==none) HitActor = Trace(HitLocation, HitNormal, p.pawn.location+p.Pawn.EyePosition(), location, true);

			if(t!=myteam && t!=255 && pawn(HitActor)!=none)
			{

				if (p.pawn.health>0 && !TFPawn(p.pawn).antiprotector && vsize(p.pawn.location-location)<range) // after
				{
					if(targ==none) targ=p.pawn;
					else if(vsize(p.location-location) < vsize(targ.location-location) && frand()<0.75) targ=p.pawn;
                }
			}
		}

		shoot(targ);
    }

    simulated function Shoot(actor targ)
    {
	    local Projectile p;
	    local Vector FireSpot;
	    local float adjust;
	    local rotator aimerror;

	    if(targ==none) return;

	    FireSpot = targ.Location + targ.Velocity*VSize(targ.Location-location)/(3250+rand(250));

        if(frand()>0.25 && pawn(targ)!=none)
        {
            if(frand()<0.5) adjust=pawn(targ).EyeHeight*frand();
            else adjust=pawn(targ).EyeHeight;

            if (FastTrace(FireSpot+adjust*vect(0,0,1),location))
                FireSpot += adjust*vect(0,0,1);
        }

        setrotation(Rotator(firespot - location));

        //FireSpot.Z+=16;  //AIM AT LEGS

        if(myteam==0)
            p=Spawn(class'TaskForces.tfGuardProj',self,,location,Rotator(FireSpot-location)+aimerror);
        else
            p=Spawn(class'TaskForces.tfGuardProjB',self,,location,Rotator(FireSpot-location)+aimerror);

        if(grade>=2)
        {
            tfGuardProj(p).seeking=targ;
            tfGuardProj(p).seekadjust=adjust;
        }
    }
}

defaultproperties
{
     MaxHealth=1500.000000
     chunkcount=6
     BuildingName="Protector"
     NameIndex=2
     directional=True
     teamflareDS=0.900000
     StaticMesh=StaticMesh'TFMedia.Buildings.tfGuardian'
     AmbientSound=Sound'TFMedia.Buildings.tfGuardianAmb'
     DrawScale=1.200000
}
